/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.li.temperature.data;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.android.li.temperature.data.Stopwatch.State;

import static com.android.li.temperature.data.Stopwatch.State.RESET;

/**
 * This class encapsulates the transfer of data between {@link Stopwatch} and
 * {@link Lap} domain objects and their permanent storage in
 * {@link SharedPreferences}.
 */
final class StopwatchDAO {

    // Key to a preference that stores the state of the stopwatch.
    private static final String STATE = "sw_state";

    // Key to a preference that stores the last start time of the stopwatch.
    private static final String LAST_START_TIME = "sw_start_time";

    // Key to a preference that stores the accumulated elapsed time of the
    // stopwatch.
    private static final String ACCUMULATED_TIME = "sw_accum_time";

    // Lazily instantiated and cached for the life of the application.
    private static SharedPreferences sPrefs;

    private StopwatchDAO() {
    }

    /**
     * @return the stopwatch from permanent storage or a reset stopwatch if none
     *         exists
     */
    public static Stopwatch getStopwatch(Context context) {
        final SharedPreferences prefs = getSharedPreferences(context);
        final int stateIndex = prefs.getInt(STATE, RESET.ordinal());
        final State state = State.values()[stateIndex];
        final long lastStartTime = prefs.getLong(LAST_START_TIME, Long.MIN_VALUE);
        final long accumulatedTime = prefs.getLong(ACCUMULATED_TIME, 0);
        return new Stopwatch(state, lastStartTime, accumulatedTime);
    }

    /**
     * @param stopwatch
     *            the last state of the stopwatch
     */
    public static void setStopwatch(Context context, Stopwatch stopwatch) {
        final SharedPreferences prefs = getSharedPreferences(context);
        final SharedPreferences.Editor editor = prefs.edit();

        if (stopwatch.isReset()) {
            editor.remove(STATE).remove(LAST_START_TIME).remove(ACCUMULATED_TIME);
        } else {
            editor.putInt(STATE, stopwatch.getState().ordinal()).putLong(LAST_START_TIME, stopwatch.getLastStartTime()).putLong(ACCUMULATED_TIME, stopwatch.getAccumulatedTime());
        }

        editor.apply();
    }

    private static SharedPreferences getSharedPreferences(Context context) {
        if (sPrefs == null) {
            sPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        }

        return sPrefs;
    }
}
